package com.migu.contacts.main.countdown;

import static com.migu.contacts.net.Service.API;

import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.BaseAdapter;
import com.frank.libandroid.BottomPopup;
import com.frank.libandroid.DataStore;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.butterknife.BindAdapter;
import com.frank.libandroid.butterknife.OnClick;
import com.github.gzuliyujiang.wheelview.contract.WheelFormatter;
import com.lxj.xpopup.XPopup;
import com.migu.contacts.R;
import com.migu.contacts.bean.ContactBean;
import com.migu.contacts.bean.DetailBean;
import com.migu.contacts.bean.GroupBean;
import com.migu.contacts.bean.MomentBean;
import com.migu.contacts.databinding.ActivityAddMomentBinding;
import com.migu.contacts.databinding.ItemContactEditContactBinding;
import com.migu.contacts.databinding.ItemCreateTodoBinding;
import com.migu.contacts.databinding.ItemCreateTodoImportanceBinding;
import com.migu.contacts.databinding.ItemSelectGroupDetailBinding;
import com.migu.contacts.databinding.PopTargetDateBinding;
import com.migu.contacts.databinding.PopTodoImportanceBinding;
import com.migu.contacts.main.ContactSelectActivity;
import com.migu.contacts.main.todo.CreateTodoActivity;
import com.migu.contacts.util.AvatarUtil;
import com.migu.contacts.widget.Label2;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class AddMomentActivity extends BaseActivity<ActivityAddMomentBinding> {
    @BindAdapter(R.id.recyclerView2)
    BaseAdapter<ItemCreateTodoBinding, Label2.LabelBean> midAdapter = new BaseAdapter<ItemCreateTodoBinding, Label2.LabelBean>() {
        @OnClick(R.id.iv_arrow)
        public void iv_arrow(Label2.LabelBean bean, int position) {
            if (position == 0) {
                showImportancePop(bean);
            }
        }
    };

    private void showImportancePop(Label2.LabelBean bean) {
        BottomPopup bottomPopup = new BottomPopup<PopTodoImportanceBinding>(mActivity) {

            boolean isOpen = false;
            CreateTodoActivity.ImportanceBean checkedNotice = null;
            @BindAdapter(R.id.recyclerView)
            BaseAdapter<ItemCreateTodoImportanceBinding, CreateTodoActivity.ImportanceBean> noticeAdapter = new BaseAdapter<ItemCreateTodoImportanceBinding, CreateTodoActivity.ImportanceBean>() {
                @OnClick(R.id.checkBox)
                public void checkBox(CreateTodoActivity.ImportanceBean bean) {
                    Log.d(TAG, "checkBox() called with: bean = [" + bean.checked + "]");
                    if (bean.checked) {
                        isOpen = true;
                        checkedNotice = bean;
                        for (CreateTodoActivity.ImportanceBean noticeBean : list) {
                            noticeBean.checked = false;
                        }
                        bean.checked = true;
                        notifyDataSetChanged();
                        mBinding.noNotice.setChecked(false);
                    }
                }
            };

            @OnClick(R.id.close)
            public void quit() {
                dismiss();
            }

            @OnClick(R.id.noNotice)
            public void noNotice() {
                if (mBinding.noNotice.isChecked()) {
                    for (CreateTodoActivity.ImportanceBean noticeBean : list) {
                        noticeBean.checked = false;
                    }
                    noticeAdapter.notifyDataSetChanged();
                    isOpen = false;
                }
            }

            List<CreateTodoActivity.ImportanceBean> list = new ArrayList<>();

            //            重复周期类型 1=天 2=周 3=月 4=年
            @Override
            protected void initView() {
                super.initView();
                list.add(new CreateTodoActivity.ImportanceBean("高", R.drawable.label_h, 3));
                list.add(new CreateTodoActivity.ImportanceBean("中", R.drawable.label_m, 2));
                list.add(new CreateTodoActivity.ImportanceBean("低", R.drawable.label_l, 1));
                noticeAdapter.setData(list);
                mBinding.noNotice.setChecked(true);
            }

            @OnClick(R.id.confirm)
            public void confirm() {
//                mNoticeBean = checkedNotice;
                if (isOpen) {
                    bean.right = checkedNotice.name;
                    momentBean.momentsMatter = checkedNotice.value;
                } else {
                    momentBean.momentsMatter = 0;
                    bean.right = "无";
                }
                midAdapter.notifyDataSetChanged();
                dismiss();
            }

        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }

    DetailBean todoBean = new DetailBean();
    MomentBean momentBean = new MomentBean();


    @BindAdapter(R.id.recyclerView)
    BaseAdapter<ItemContactEditContactBinding, ContactBean> contactAdapter = new BaseAdapter<ItemContactEditContactBinding, ContactBean>() {
        @Override
        protected void onBindData(ItemContactEditContactBinding binding, ContactBean bean, int position) {
            super.onBindData(binding, bean, position);
            binding.avatar.setImageResource(AvatarUtil.getAvatar(bean.gender));
            LinearLayoutManager layoutManager = (LinearLayoutManager) binding.groupRecyclerView.getLayoutManager();
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            BaseAdapter<ItemSelectGroupDetailBinding, GroupBean> groupBeanBaseAdapter = new BaseAdapter<ItemSelectGroupDetailBinding, GroupBean>() {
            };
            binding.groupRecyclerView.setAdapter(groupBeanBaseAdapter);
            groupBeanBaseAdapter.setData(bean.relationGroupList);
        }
    };
    List<ContactBean> contactBeanList = new ArrayList<>();

    public void goContacts(View view) {
        startActivityForResult(ContactSelectActivity.class, new Runnable() {
            @Override
            public void run() {
                Set<ContactBean> contactBeans = DataStore.get(ContactSelectActivity.KEY_CONTACTS);
                if (contactBeans != null) {
                    contactBeanList.clear();
                    contactBeanList.addAll(contactBeans);
                    if (contactBeanList.isEmpty()) {
                        mBinding.selectPerson.setVisibility(View.VISIBLE);
                        mBinding.addContacts.setVisibility(View.GONE);
                    } else {
                        mBinding.selectPerson.setVisibility(View.GONE);
                        mBinding.addContacts.setVisibility(View.VISIBLE);
                    }
                    contactAdapter.setData(contactBeanList);
                }
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        mBinding.setBean(momentBean);
        List<Label2.LabelBean> mid = new ArrayList<>();
        mid.add(new Label2.LabelBean("重要程度", "无", 0, true));
        midAdapter.setData(mid);
        todoBean.data = momentBean;
        todoBean.eventType = 6;
        todoBean.contactIds = new ArrayList<>();
        if (contactBeanList.isEmpty()) {
            mBinding.selectPerson.setVisibility(View.VISIBLE);
            mBinding.addContacts.setVisibility(View.GONE);
        } else {
            mBinding.selectPerson.setVisibility(View.GONE);
            mBinding.addContacts.setVisibility(View.VISIBLE);
        }
        mBinding.date.setBean(new Label2.LabelBean("目标日", LocalDate.now().toString(), 0, true));
        momentBean.startTime = LocalDate.now().toString();
        mBinding.date.mBinding.ivArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDatePicker();
            }
        });
    }

    private void showDatePicker() {
        BottomPopup bottomPopup = new BottomPopup<PopTargetDateBinding>(mActivity) {
            @OnClick(R.id.close)
            public void quit() {
                dismiss();
            }

            @OnClick(R.id.confirm)
            public void confirm() {
//                DateWheelLayout date = mBinding.date;
//                LocalDate localDate = LocalDate.of(date.getSelectedYear(), date.getSelectedMonth(), date.getSelectedDay());
//                String string = localDate.toString();
////                mBinding.date.mBinding.tvValue.setText(string);
//                momentBean.startTime = string;
                LocalDate localDate = mBinding.date.getCurrentItem();
                String string = localDate.toString();
                AddMomentActivity.this.mBinding.date.mBinding.tvValue.setText(string);
                momentBean.startTime = string;
                dismiss();
            }

            @Override
            protected void initView() {
                super.initView();
                LocalDate now = LocalDate.now();
                List<LocalDate> list = new ArrayList<>();
                for (int i = -200; i < 200; i++) {
                    LocalDate localDate = now.plusDays(i);
                    list.add(localDate);
                }
                mBinding.date.setData(list);
                mBinding.date.setDefaultValue(now);
                mBinding.date.setFormatter(new WheelFormatter() {
                    @Override
                    public String formatItem(@NonNull Object item) {
                        LocalDate localDate = (LocalDate) item;
                        if (localDate == now) {
                            return localDate.toString() + "(今天)";
                        }
                        return localDate.toString();
                    }
                });
            }
        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }

    public void save(View view) {
        for (ContactBean contactBean : contactBeanList) {
            todoBean.contactIds.add(contactBean.id);
        }
        API.calendarCreate(todoBean).watch(this, new HttpCallback<Boolean>() {
            @Override
            protected void onSuccess(Boolean aBoolean) {
                finish();
            }
        });
    }
}